
#include "StatePause.h"

#include "StateMainMenu.h"
#include "StateManager.h"
#include "BackgroundManager.h"
#include "AudioUtils.h"
#include "defs.h"
#include "TextEntity.h"
#include "RenderableEntity.h"
#include "ScaleEffect.h"
#include "TimeLineEffect.h"
#include "ProgressManager.h"
#include "MusicManager.h"

StatePause::StatePause()
{
}

StatePause::~StatePause()
{
}

void StatePause::loadContent()
{
    m_bg = new RenderableEntity("fade", Vector2(0.0f, 0.0f));
    m_bg->setColor(COLOR_BLACK);
    m_bg->setScale(Vector2(640, 960));
    m_bg->setAlpha(0);
    
    m_oRibbon = new RenderableEntity("MENU_0009_ribbon", Vector2(320, 870));
    m_oRibbon->setCentered();
    m_oRibbon->setLiving(true);

	m_tPause = new TextEntity("Pause", FONT_NAME, 64, Vector2(320, 890), 1, 1);
    m_tPause->setCentered();
    m_tPause->setColor(COLOR_PINK_TEXT);
    m_tPause->setShadow(true, COLOR_PINK_DARK);
    
    m_oMusic = new TextEntity("Music", FONT_NAME, 48, Vector2(250, 700), 1, 1);
    m_oMusic->setCentered();
    m_oMusic->setShadow();
    m_oMusicBG = new RenderableEntity("checkbox", Vector2(420, 700));
	m_oMusicBG->setCentered();
    m_oMusicBG->setupButton(Vector2(1.5f, 1.5f));
    m_oMusicMark = new RenderableEntity("tick", Vector2(430, 710));
	m_oMusicMark->setCentered();
    
    m_oSfx = new TextEntity("Sounds", FONT_NAME, 48, Vector2(250, 550), 1, 1);
    m_oSfx->setCentered();
    m_oSfx->setShadow();
    m_oSfxBG = new RenderableEntity("checkbox", Vector2(420, 550));
	m_oSfxBG->setCentered();
    m_oSfxBG->setupButton(Vector2(1.5f, 1.5f));
    m_oSfxMark = new RenderableEntity("tick", Vector2(430, 560));
	m_oSfxMark->setCentered();
    
    m_oAccelerometer = new TextEntity("Accelerometer", FONT_NAME, 48, Vector2(230, 400), 1, 1);
    m_oAccelerometer->setCentered();
    m_oAccelerometer->setShadow();
    m_oAccelerometer->setColor(COLOR_GREY);
    m_oAccelerometerDisabled = new TextEntity("Buy it in the Shop first!", FONT_NAME, 24, Vector2(230, 360), 1, 1);
    m_oAccelerometerDisabled->setCentered();
    m_oAccelerometerDisabled->setShadow();
    m_oAccelerometerDisabled->setColor(COLOR_GREY);
    m_oAccelerometerBG = new RenderableEntity("checkbox", Vector2(420, 400));
	m_oAccelerometerBG->setCentered();
    m_oAccelerometerBG->setupButton(Vector2(1.5f, 1.5f));
    m_oAccelerometerMark = new RenderableEntity("tick", Vector2(430, 410));
	m_oAccelerometerMark->setCentered();
    
    m_oLeftBg = new RenderableEntity("buttonyesNO", Vector2(200, 250));
    m_oLeftBg->setScale(Vector2(2.0f, 1.0f));
    m_oLeftBg->setCentered();
    
    m_tMainMenu = new TextEntity("Main Menu", FONT_NAME, 32, Vector2(200, 250), 1, 1);
    m_tMainMenu->setCentered();
    m_tMainMenu->changeColorWhenClicked(true);
    m_tMainMenu->setupButton(Vector2(0.9f, 1.5f));
    
    m_oRightBg = new RenderableEntity("buttonyesNO", Vector2(440, 250));
    m_oRightBg->setScale(Vector2(2.0f, 1.0f));
    m_oRightBg->setCentered();
    
    m_tContinue = new TextEntity("Continue", FONT_NAME, 32, Vector2(440, 250), 1, 1);
    m_tContinue->setCentered();
    m_tContinue->changeColorWhenClicked(true);
    m_tContinue->setupButton(Vector2(1.2f, 1.5f));
    
	m_ePauseEnter = new TimeLineEffect(false, false);
	m_ePauseEnter->addEffect(new FadeEffect(m_bg, 0.10, false, false, 0.0f, 0.7f), 0);
	m_ePauseEnter->addInEffect(m_oRibbon);
	m_ePauseEnter->addInEffect(m_tPause);
	m_ePauseEnter->addInEffect(m_oLeftBg);
	m_ePauseEnter->addInEffect(m_oRightBg);
	m_ePauseEnter->addInEffect(m_tContinue);
	m_ePauseEnter->addInEffect(m_tMainMenu);
	m_ePauseEnter->addInEffect(m_oMusic);
	m_ePauseEnter->addInEffect(m_oMusicBG);
	m_ePauseEnter->addInEffect(m_oMusicMark);
	m_ePauseEnter->addInEffect(m_oSfx);
	m_ePauseEnter->addInEffect(m_oSfxBG);
	m_ePauseEnter->addInEffect(m_oSfxMark);
	m_ePauseEnter->addInEffect(m_oAccelerometer);
	m_ePauseEnter->addInEffect(m_oAccelerometerDisabled);
	m_ePauseEnter->addInEffect(m_oAccelerometerBG);
	m_ePauseEnter->addInEffect(m_oAccelerometerMark);
    
	m_ePauseExit = new TimeLineEffect(false, false);
	m_ePauseExit->addEffect(new FadeEffect(m_bg, 0.10, false, false, 0.0f), 0);
	m_ePauseExit->addOutEffect(m_oRibbon);
	m_ePauseExit->addOutEffect(m_tPause);
	m_ePauseExit->addOutEffect(m_oLeftBg);
	m_ePauseExit->addOutEffect(m_oRightBg);
	m_ePauseExit->addOutEffect(m_tContinue);
	m_ePauseExit->addOutEffect(m_tMainMenu);
	m_ePauseExit->addOutEffect(m_oMusic);
	m_ePauseExit->addOutEffect(m_oMusicBG);
	m_ePauseExit->addOutEffect(m_oMusicMark);
	m_ePauseExit->addOutEffect(m_oSfx);
	m_ePauseExit->addOutEffect(m_oSfxBG);
	m_ePauseExit->addOutEffect(m_oSfxMark);
	m_ePauseExit->addOutEffect(m_oAccelerometer);
	m_ePauseExit->addOutEffect(m_oAccelerometerDisabled);
	m_ePauseExit->addOutEffect(m_oAccelerometerBG);
	m_ePauseExit->addOutEffect(m_oAccelerometerMark);
}

void StatePause::unloadContent()
{
    delete m_bg;
    
    delete m_tPause;
    delete m_tContinue;
    delete m_tMainMenu;
    
    delete m_ePauseEnter;
    delete m_ePauseExit;
}

void StatePause::enter()
{
	m_eState = Enter;
	m_ePauseEnter->start(true);
    
    SaveGame sGame = ProgressManager::instance().getSavedData();
    m_oMusicMark->setRender(sGame.bMusic);
    m_oSfxMark->setRender(sGame.bSfx);
    m_oAccelerometerMark->setRender(sGame.bAccelerometer);
    
    //if(sGame.baUpgrades[ACCELEROMETER])
    if(true)
    {
        m_oAccelerometer->setColor(COLOR_WHITE);
        m_oAccelerometerDisabled->setRender(false);
    }
    else
    {
        m_oAccelerometer->setColor(COLOR_GREY);
        m_oAccelerometerDisabled->setRender(true);
    }
}

void StatePause::update(float dt)
{
	switch(m_eState)
	{
		case Enter:
			if(m_ePauseEnter->isFinished())
			{
				m_eState = Idle;
			}
			break;
            
		case Idle:
            m_oRibbon->update();
            m_tPause->setScale(m_oRibbon->getScale());
            
			m_tMainMenu->update();
			m_tContinue->update();
            
			if(m_tContinue->isClicked())
			{
				m_ePauseExit->start(true);
				m_eState = Exit;
			}
            else if(m_tMainMenu->isClicked())
            {
				m_ePauseExit->start(true);
                m_eState = ToMenu;
            }
            else if(m_oMusicBG->isClicked() || m_oMusic->isClicked())
            {
                SaveGame sGame = ProgressManager::instance().getSavedData();
                sGame.bMusic = !sGame.bMusic;
                ProgressManager::instance().setSavedData(sGame);
                m_oMusicMark->setRender(sGame.bMusic);
                
                if(sGame.bMusic)
                {
                    MusicManager::instance().setMusicVolume(MUSIC_VOLUME);
                }
                else
                {
                    MusicManager::instance().setMusicVolume(0);
                }
            }
            else if(m_oSfxBG->isClicked() || m_oSfx->isClicked())
            {
                SaveGame sGame = ProgressManager::instance().getSavedData();
                sGame.bSfx = !sGame.bSfx;
                ProgressManager::instance().setSavedData(sGame);
                m_oSfxMark->setRender(sGame.bSfx);
            }
//            else if(ProgressManager::instance().getSavedData().baUpgrades[ACCELEROMETER] && (m_oAccelerometer->isClicked() || m_oAccelerometerBG->isClicked()))
            else if( m_oAccelerometer->isClicked() || m_oAccelerometerBG->isClicked() )
            {
                SaveGame sGame = ProgressManager::instance().getSavedData();
                sGame.bAccelerometer = !sGame.bAccelerometer;
                ProgressManager::instance().setSavedData(sGame);
                m_oAccelerometerMark->setRender(sGame.bAccelerometer);
            }
			break;
            
		case Exit:
			if(m_ePauseExit->isFinished())
			{
				StateManager::instance().popState();
			}
			break;
            
		case ToMenu:
			if(m_ePauseExit->isFinished())
			{
                BackgroundManager::instance().fadeOut();
                StateManager::instance().setState(StateMainMenu::instance());
			}
			break;
	}
}

void StatePause::render()
{
    m_bg->render();
   	
    m_oRibbon->render();
    m_tPause->render();
    
    m_oMusic->render();
    m_oMusicBG->render();
    m_oMusicMark->render();
    
    m_oSfx->render();
    m_oSfxBG->render();
    m_oSfxMark->render();
    
    m_oAccelerometer->render();
    m_oAccelerometerDisabled->render();
    m_oAccelerometerBG->render();
    m_oAccelerometerMark->render();

    m_oLeftBg->render();
    m_oRightBg->render();
    
    m_tContinue->render();
    m_tMainMenu->render();
}

void StatePause::exit()
{
}
